package com.satis.compose.get.widget


import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavGraphBuilder
import com.satis.compose.get.navigator.*
import com.satis.compose.get.navigator.rememberGetAnimatedNavController
import com.satis.compose.get.navigator.rememberGetPageController

/**
 * @author sunshaobei on 2022/9/9
 */

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun GetApp(startDestination: String,
           routeNoFoundDestination:String? = null,
           enterTransition: (AnimatedContentScope<NavBackStackEntry>.() -> EnterTransition)? = null,
           exitTransition: (AnimatedContentScope<NavBackStackEntry>.() -> ExitTransition)? = null,
           popEnterTransition: (AnimatedContentScope<NavBackStackEntry>.() -> EnterTransition)? = null,
           popExitTransition: (AnimatedContentScope<NavBackStackEntry>.() -> ExitTransition)? = null,
           destinationChangedListener: NavController.OnDestinationChangedListener? = null,
           builder: NavGraphBuilder.() -> Unit) {
    val rememberAnimatedNavController = rememberGetAnimatedNavController()
    destinationChangedListener?.let {
        rememberAnimatedNavController.addOnDestinationChangedListener(it)
    }
    val rememberGetPageController = rememberGetPageController(routeNoFoundDestination,rememberAnimatedNavController)
    CompositionLocalProvider(LocalNavHostController provides rememberAnimatedNavController, LocalPageController provides rememberGetPageController) {
       GetAnimatedNavHost(
           navController = rememberAnimatedNavController,
           startDestination = startDestination,
           builder = builder,
           enterTransition = enterTransition,
           exitTransition = exitTransition,
           popEnterTransition = popEnterTransition,
           popExitTransition = popExitTransition,
       )
    }
}
